<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\QRcode;

class Qcode extends Api {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected function _initialize() {
        
    }

    public function getQcode() {
        $url = $this->request->param('url');
        $type = $this->request->param('type');


        if($type){
           $url.='?'.base64_encode('type='.$type); 
        }
        $fname = md5(time() + mt_rand(1, 9999));
        $filename = './uploads/qcode/' . $fname . '.png';

        $return_file = $this->request->domain() . '/uploads/qcode/' . $fname . '.png';
        $this->qrcode($url, $filename);
//        watermarklogo($filename);
        $img_num = \think\Db::table('fa_pic')->count();
        $this->success('生成成功', ['qcode'=>$return_file,'img_num'=>$img_num]);
    }

    /**
     * host 生成链接地址
     * level 容错级别 
     * size 图片大小
     */
    public function qrcode($url = '', $filename = '', $level = 1, $size = 1.64) {

        $errorCorrectionLevel = intval($level); //容错级别 
//        $matrixPointSize = intval($size); //生成图片大小 
        //生成二维码图片 
        $object = new QRcode();

        $res = $object->png($url, $filename, $errorCorrectionLevel, $size, 2, false);
//        $QR = imagecreatefromstring(file_get_contents($filename));
//
////        //输出图片    
//        imagepng($QR, $card_number . '.png');
//        imagedestroy($QR);
        return $filename;
    }

}
